<template>
    <div class="login">
        <div class="header">
            <div class="title">登录</div>
            <div class="sub-title">欢迎回来</div>
            <div class="skip-btn">
                <van-button plain hairline type="primary" size="small" @click="skipLogin">跳过登录</van-button>
            </div>
        </div>
        <van-form @submit="onSubmit" class="form">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="用户名" placeholder="请输入用户名"
                    :rules="[{ required: true, message: '请输入用户名' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="请输入密码"
                    :rules="[{ required: true, message: '请输入密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit" :loading="loading">
                    登录
                </van-button>
            </div>
            <div class="links">
                <router-link to="/register">没有账号？去注册</router-link>
            </div>
        </van-form>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { showToast } from 'vant'
import { useRouter, useRoute } from 'vue-router'
import useUserStore from '@/stores/modules/user'

const username = ref('')
const password = ref('')
const loading = ref(false)
const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 提交表单
const onSubmit = async () => {
    try {
        loading.value = true
        const result = await userStore.login(username.value, password.value)

        if (result.success) {
            showToast({
                type: 'success',
                message: '登录成功',
            })

            // 如果存在redirect参数，登录后重定向到该路径
            const redirect = route.query.redirect || '/home'
            router.replace(redirect)
        } else {
            showToast({
                type: 'fail',
                message: result.message || '用户名或密码错误',
            })
        }
    } catch (error) {
        console.error('登录错误:', error)
        showToast({
            type: 'fail',
            message: '登录失败，请重试',
        })
    } finally {
        loading.value = false
    }
}

// 跳过登录
const skipLogin = () => {
    showToast({
        type: 'success',
    })
    // 跳转到首页
    router.replace('/home')
}
</script>

<style lang="less" scoped>
.login {
    padding: 20px;
    height: 100vh;
    box-sizing: border-box;
    background: linear-gradient(to bottom, #ff9854 0%, #ff9854 10%, #ffffff 100%);

    .header {
        margin: 40px 0;
        position: relative;

        .title {
            font-size: 28px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #fff;
        }

        .sub-title {
            font-size: 16px;
            color: rgba(255, 255, 255, 0.8);
        }

        .skip-btn {
            position: absolute;
            top: 0;
            right: 0;

            :deep(.van-button) {
                border: none;
                color: #fff;
                font-size: 14px;
                background-color: transparent;
            }
        }
    }

    .form {
        margin-top: 40px;

        :deep(.van-cell-group--inset) {
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        }
    }

    .links {
        margin-top: 16px;
        text-align: center;

        a {
            color: #fff;
            text-decoration: none;
            font-weight: 500;
        }
    }
}
</style>